<template>
    <d2-container>

                <el-dialog
                title="订单详情"
                :visible.sync="dialogVisible"
                width="1000px"
                height='300px'
        >

             <div id="printMe" style="width: 910.8px;height:258px;border:solid 1px black">
                <div style="padding-left:20px;padding-right: 20px;padding-top: 10px">
                    <div style="font-size:14px;font-weight: 5px"><span>校区:{{printData.school}}</span></div>
                    <div  style="font-size:14px;font-weight: 5px;margin-top:5px">时间:{{printData.date}}</div>
                    <div  style="font-size:14px;font-weight: 5px;margin-top:5px">经办人:{{printData.agent}}</div>
                    <div  style="font-size:14px;font-weight: 5px;margin-top:5px">类型:{{printData.category}}</div>
                    <div  style="font-size:14px;font-weight: 5px;margin-top:5px">进/出:{{printData.inout}}</div>
                    <div  style="font-size:14px;font-weight: 5px;margin-top:5px">金额:{{printData.total}}元</div>
                    <div  style="font-size:14px;font-weight: 5px;margin-top:5px">备注:{{printData.comment}}</div>
                    
                </div>
            </div>


            <span slot="footer" class="dialog-footer">
                <el-button @click='printabc()'>打印</el-button>
                <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
            </span>
        </el-dialog>


        <el-form :inline="true" :model="dataSearch" ref="dataSearch">

           <!--  <el-form-item label="其他进出款项" prop="name">
                <el-input v-model="dataSearch.name" placeholder="经办人"></el-input>
            </el-form-item> -->

           <!--  <el-form-item>
                <el-button type="primary" @click="formSubmit">
                    <d2-icon name="search"/>
                    查询
                </el-button>
            </el-form-item>
            <el-form-item>
                <el-button @click="formReset">
                    <d2-icon name="refresh"/>
                    重置
                </el-button>
            </el-form-item> -->

            <el-select
            style="position: absolute;left:1.5vw;top:1.5vw;z-index: 100"
            v-model="school"
            filterable
            placeholder="所属校区"
            @change="schoolSelected"
            v-if="headquarter == 1"
          >
            <el-option
              v-for="item in schoolList"
              :key="item._id"
              :label="item.name"
              :value="item._id"
            ></el-option>
          </el-select>

            <a href="#/change-expense-record">
                <el-button type="primary" style="float:right;margin-bottom: 2vw" icon="el-icon-plus">新建进出款项</el-button>
            </a>
        </el-form>
        <el-table :data="dataList" border style="width: 100%">
            <el-table-column prop="date" label="时间"></el-table-column>
            <el-table-column prop="agent" label="经办人"></el-table-column>
            <el-table-column prop="total" label="金额"></el-table-column>
            <el-table-column prop="category" label="类型"></el-table-column>

            <el-table-column prop="inout" label="进/出"></el-table-column>
            <el-table-column prop="comment" label="备注"></el-table-column>

            <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                    <el-button @click="changeClick(scope.row)" type="text" size="small">修改</el-button>
                    <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
                    <el-button @click="print(scope.row)" type="text" size="small">打印</el-button>

                </template>
                
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="total,sizes,prev, pager, next,jumper"
                :page-size="pageSize"
                :current-page="currentPage"
                :total="total"
                @current-change="handleCurrentChange"
                @size-change="handlerSizeChange"
                @next-click="handleNextPage"
                @prev-click="handlePrevClick"
        ></el-pagination>
    </d2-container>
</template>

<script>
    import request from "@/plugin/axios";
    import {mapState} from "vuex";
    import formatTime from "@/libs/util.formatTime.js";

    export default {
        data() {
            return {
                dataList: [],
                total: 100,
                pageSize: 20,
                currentPage: 1,
                courseType: [''],
                school: undefined,
                dataSearch: {},
                courseTypeDetail: [],
                headquarter : undefined,
                schoolList : [],
                printData : {},
                dialogVisible : false
            };
        },
        computed: {
            ...mapState('d2admin/user', ['info']),
        },
        methods: {
            printabc(div){
            var PW =  window.open('', '', 'left=0,top=0,width=600,height=848.6,toolbar=0,scrollbars=0,status=0');
            // 获取div中的html内容
            PW.document.write(document.getElementById('printMe').innerHTML);
            PW.document.close();
            PW.focus();
            PW.print();
            PW.close();

            return false;
        },
        print(row){
            console.log('dsds')
            this.printData = row
            this.dialogVisible = true
        },
            formSubmit() {
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.school);
            },
            formReset() {
                this.$refs.dataSearch.resetFields();
            },
            deleteClick(e) {
                this.$confirm("此操作将删除该条进出款项, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        request({
                            url: "/data/deleteinout",
                            method: "post",
                            data: { _id: e._id }
                        }).then(res => {
                            if(res.code ==200){
                                this.$message({
                                    message: "删除成功"
                                });
                                this.getDataList(this.school);
                            }
                        });

                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },
            changeClick(e){
                this.$router.push('/change-expense-record?id=' + e._id)
            },


            handleCurrentChange(currentPage) {
                this.currentPage = currentPage
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.school);
            },
            handlerSizeChange(pageSize) {
                this.pageSize = pageSize
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.school);
            },
            handleNextPage() {
                this.currentPage = this.currentPage + 1
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.school);
            },
            handlePrevClick() {
                this.currentPage = this.currentPage - 1
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.school);
            },

            getSchoolList(page, pageSize) {



      let data = {};
      (data.page = page), (data.pageSize = pageSize);
      request({
        url: "/school/getSchoolList",
        method: "post",
        data
      }).then(res => {
        // this.school = this.info.school[0]._id
        this.schoolList = [{name:'全部',_id:'all'}]
        this.schoolList = this.schoolList.concat(res.data.school)
        // 返回数据
      });


    },

                schoolSelected() {
      // this.getDepartmentList(1, 99999);
            var ob = this.schoolList.map(function(e){return e._id}).indexOf(this.school)
            this.school = this.schoolList[ob]._id
            if (this.schoolList[ob]._id == 'all'){
                this.getDataList('all')
            }
            else{
                this.getDataList(this.schoolList[ob]._id)
            }
        },
            


            getDataList(data) {
                // this.dataSearch.limitSchool = this.info.school.map(value => value._id);
                // data.limitSchool = this.dataSearch.limitSchool;
                let datax = {}
                datax.pageSize = 9999
                datax.page = 1
                if (data != 'all'){
                    datax.school = data
                }
                // console.log(datax,'datax',data)
                request({
                    url: "/data/getinoutList",
                    method: "post",
                    data: datax
                }).then(res => {
                    var temp = []
                    for (var i = 0 ; i < res.data.data.length ; i++){
                        var temp2 = {}
                        temp2.date = formatTime.formatTime(res.data.data[i].date).split(' ')[0]
                        temp2.agent = res.data.data[i].staff.name
                        temp2.total = res.data.data[i].total
                        temp2.inout = res.data.data[i].type == 0 ? '支出' : '收入'
                        temp2.category = res.data.data[i].category
                        temp2.comment = res.data.data[i].comment
                        temp2._id = res.data.data[i]._id
                        temp2.school = res.data.data[i].school.name
                        temp.push(temp2)
                    }
                    this.dataList = temp
                    this.total = res.data.total;
                    this.currentPage = res.data.currentPage;
                })
            },
            load(){
                this.school = this.info.school[0]._id
                if ((this.info.school[0].headquarter == 1) || (this.info.school[0].headquarter == 2)){
                    this.headquarter = 1
                    this.getSchoolList(1,99999)
                }
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.school);
                }
        },
        beforeRouteEnter(to,from,next){
            console.log(to.meta.refresh == 'a','tee',to.meta.refresh)
            if (to.meta.refresh == 'a'){
                to.meta.refresh = undefined
                next(v=>{
                v.load()
                })
            }
            else{
                next()
            }

        },

        created() {
            this.load()
        }
    };
</script>
